#include "menu.h"

char* logo8 = "                         __                    __                ";
char* logo9 = " /'\\_/`\\                /\\ \\__                /\\ \\               ";
char* logo10 = "/\\      \\  __  __   ____\\ \\ ,_\\    __      ___\\ \\ \\___      __   ";
char* logo11 = "\\ \\ \\__\\ \\/\\ \\/\\ \\ /',__\\\\ \\ \\/  /'__`\\   /'___\\ \\  _ `\\  /'__`\\ ";
char* logo12 = " \\ \\ \\_/\\ \\ \\ \\_\\ \\\\__, `\\\\ \\ \\_/\\ \\L\\.\\_/\\ \\__/\\ \\ \\ \\ \\/\\  __/ ";
char* logo13 = "  \\ \\_\\\\ \\_\\ \\____//\\____/ \\ \\__\\ \\__/.\\_\\ \\____\\\\ \\_\\ \\_\\ \\____\\";

char* logo1 = " ____    ___                            __      ";
char* logo2 = "/\\  _`\\ /\\_ \\                          /\\ \\__   ";
char* logo3 = "\\ \\ \\L\\ \\//\\ \\      __      ___      __\\ \\ ,_\\  ";
char* logo4 = " \\ \\ ,__/ \\ \\ \\   /'__`\\  /' _ `\\  /'__`\\ \\ \\/  ";
char* logo5 = "  \\ \\ \\/   \\_\\ \\_/\\ \\L\\.\\_/\\ \\/\\ \\/\\  __/\\ \\ \\_ ";
char* logo6 = "   \\ \\_\\   /\\____\\ \\__/.\\_\\ \\_\\ \\_\\ \\____\\\\ \\__\\";
char* logo7 = "    \\/_/   \\/____/\\/__/\\/_/\\/_/\\/_/\\/____/ \\/__/";

int menu(){
	extern ConfigInfo conf;
	extern Queue keys_queue;
	extern int last_sc;
	int option=0;
	leave_graphics(conf.map);
	fillScreen(NORMAL);
	printLogo();
	printMenu(option);

	mask_pic(KBD_IRQ);
	last_sc = queueGet(&keys_queue);
	while(1){
		if(!queueEmpty(&keys_queue))
		{
			last_sc = queueGet(&keys_queue);
			if(last_sc == KEY_ENTER){
				break;			
			}
			if(last_sc == KEY_UP){
				option--;
				if(option==-1)
					option = 3;
				printMenu(option);
			} else if(last_sc == KEY_DOWN){
				option++;
				if(option==4)
					option = 0;
				printMenu(option);
			}
		}
		unmask_pic(KBD_IRQ);
	}
	return option;
}


void printMenu(option){
	switch(option){
		case 0:{
			printStringAt("  Novo Jogo  ", 8, 14, LIGHTGRAY_BACK|STRONG);
			printStringAt("  Pontuacoes ", 8, 15, NORMAL);
			printStringAt("   Opcoes    ", 8, 16, NORMAL);
			printStringAt("    Sair     ", 8, 17, NORMAL);	
			break;
		}	
		case 1:{
			printStringAt("  Novo Jogo  ", 8, 14, NORMAL);
			printStringAt("  Pontuacoes ", 8, 15, LIGHTGRAY_BACK|STRONG);
			printStringAt("   Opcoes    ", 8, 16, NORMAL);
			printStringAt("    Sair     ", 8, 17, NORMAL);	
			break;
		}
		case 2:{
			printStringAt("  Novo Jogo  ", 8, 14, NORMAL);
			printStringAt("  Pontuacoes ", 8, 15, NORMAL);
			printStringAt("   Opcoes    ", 8, 16, LIGHTGRAY_BACK|STRONG);
			printStringAt("    Sair     ", 8, 17, NORMAL);	
			break;		
		}
		case 3:{
			printStringAt("  Novo Jogo  ", 8, 14, NORMAL);
			printStringAt("  Pontuacoes ", 8, 15, NORMAL);
			printStringAt("   Opcoes    ", 8, 16, NORMAL);
			printStringAt("    Sair     ", 8, 17, LIGHTGRAY_BACK|STRONG);	
			break;		
		}
	}
}

void printOptions(option){
	switch(option){
		case 0:{
				printStringAt("  Configurar teclas  ", 8, 14, LIGHTGRAY_BACK|STRONG);
				printStringAt("  Ligar/desligar som ", 8, 15, NORMAL);
				printStringAt("       Voltar        ", 8, 16, NORMAL);
				break;
			}	
		case 1:{
				printStringAt("  Configurar teclas  ", 8, 14, NORMAL);
				printStringAt("  Ligar/desligar som ", 8, 15, LIGHTGRAY_BACK|STRONG);
				printStringAt("       Voltar        ", 8, 16, NORMAL);
				break;
			}
		case 2:{
				printStringAt("  Configurar teclas  ", 8, 14, NORMAL);
				printStringAt("  Ligar/desligar som ", 8, 15, NORMAL);
				printStringAt("       Voltar        ", 8, 16, LIGHTGRAY_BACK|STRONG);
				break;
			}
	}
}

void printLogo(){
	extern char* logo1;
	extern char* logo2;
	extern char* logo3;
	extern char* logo4;
	extern char* logo5;
	extern char* logo6;
	extern char* logo7;
	
	extern char* logo8;
	extern char* logo9;
	extern char* logo10;
	extern char* logo11;
	extern char* logo12;
	extern char* logo13;
	
	fillScreen(NORMAL);
	printStringAt(logo1, 2, 0, NORMAL);
	printStringAt(logo2, 2, 1, NORMAL);
	printStringAt(logo3, 2, 2, NORMAL);
	printStringAt(logo4, 2, 3, NORMAL);
	printStringAt(logo5, 2, 4, NORMAL);
	printStringAt(logo6, 2, 5, NORMAL);
	printStringAt(logo7, 2, 6, NORMAL);
	
	printStringAt(logo8, 8, 7, NORMAL);
	printStringAt(logo9, 8, 8, NORMAL);
	printStringAt(logo10,8, 9, NORMAL);
	printStringAt(logo11, 8, 10, NORMAL);
	printStringAt(logo12, 8, 11, NORMAL);
	printStringAt(logo13, 8, 12, NORMAL);	
			
}

void toggleSound(){
	extern ConfigInfo conf;
	extern Queue keys_queue;
	extern int last_sc;
	int option=0;
	fillScreen(NORMAL);
	printLogo();
	printSoundToggle(option);
	mask_pic(KBD_IRQ);
	last_sc = queueGet(&keys_queue);
	while(1){
		if(!queueEmpty(&keys_queue))
		{
			last_sc = queueGet(&keys_queue);
			if(last_sc == KEY_ENTER){
				break;			
			}
			if(last_sc == KEY_UP){
				option--;
				if(option==-1)
					option = 1;
				printSoundToggle(option);
				
			} else if(last_sc == KEY_DOWN){
				option++;
				if(option==2)
					option = 0;
				printSoundToggle(option);
			}
		}
		unmask_pic(KBD_IRQ);
	}
	if(option==0){
		*(conf.playState)=READY;
	}else{
		*(conf.playState)=STOP;
	}
}

void printSoundToggle(option){
	switch(option){
		case 0:{
			printStringAt("   Ligar    ", 8, 14, LIGHTGRAY_BACK|STRONG);
			printStringAt("  Desligar  ", 8, 15, NORMAL);
			break;
		}	
		case 1:{
			printStringAt("   Ligar    ", 8, 14, NORMAL);
			printStringAt("  Desligar  ", 8, 15, LIGHTGRAY_BACK|STRONG);
			break;
		}
	}
}

void configKeys(){
	extern ConfigInfo conf;
	extern Queue keys_queue;
	extern int last_sc;
	fillScreen(NORMAL);
	printLogo();
	printStringAt("Tecla de saltar:(clicar)", 8, 14, LIGHTGRAY_BACK|STRONG);
	do{
		if(!queueEmpty(&keys_queue))
		{
			last_sc = queueGet(&keys_queue);
			if(!(last_sc>>7)){
				conf.jumpKey=last_sc;
				break;
			}
		}
	}
	while(1);
	printStringAt("Tecla para esquerda:(clicar)", 8, 15, LIGHTGRAY_BACK|STRONG);
	do{
		if(!queueEmpty(&keys_queue))
		{
			last_sc = queueGet(&keys_queue);
			if(!(last_sc>>7)){
				conf.leftKey=last_sc;
				break;
			}
		}
	}
	while(1);
	printStringAt("Tecla para direita:(clicar)", 8, 16, LIGHTGRAY_BACK|STRONG);
	do{
		if(!queueEmpty(&keys_queue))
		{
			
			last_sc = queueGet(&keys_queue);
			if(!(last_sc>>7)){
				conf.rightKey=last_sc;
				break;
			}
		}
	}
	while(1);	
}

int options(){
	extern ConfigInfo conf;
	extern Queue keys_queue;
	extern int last_sc;
	int option=0;
	fillScreen(NORMAL);
	printLogo();
	printOptions(option);
	
	mask_pic(KBD_IRQ);
	last_sc = queueGet(&keys_queue);
	while(1){
		if(!queueEmpty(&keys_queue))
		{
			//obter o proximo elemento da queue e retira-lo
			last_sc = queueGet(&keys_queue);
			
			if(last_sc == KEY_ENTER){
				break;			
			}
			
			if(last_sc == KEY_UP){
				option--;
				if(option==-1)
					option = 2;
				printOptions(option);
				
			} else if(last_sc == KEY_DOWN){
				option++;
				if(option==3)
					option = 0;
				printOptions(option);
			}
		}
		unmask_pic(KBD_IRQ);
	}
	return option;
}
